package com.ruoyi.project.cms.menu.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.web.domain.Ztree;
import com.ruoyi.project.cms.menu.domain.SysMenu;
import com.ruoyi.project.cms.menu.mapper.SysMenuMapper;
import com.ruoyi.project.cms.menu.service.SysMenuService;
import com.ruoyi.project.cms.role.domain.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 公司:
 * 作者:胖虎
 * 类名:SysMenuServiceImpl
 * 描述:
 * 时间:2020-05-19
 */

@Service
public class SysMenuServiceImpl   implements SysMenuService {

    @Autowired
    private   SysMenuMapper      menuMapper;

    /**
     * 菜单树的查询
     * @return
     */
    @Override
    public List<Ztree>  roleMenuTreeData(SysRole role) {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        List<SysMenu>   menuList = selectMenuAll();

        ztrees = initZtree(menuList, null, true);
        return ztrees;
    }

    /**
     * 查询所有的菜单
     * @param menu
     * @return
     */
    @Override
    public List<SysMenu>  selectMenuList(SysMenu menu) {
        return  menuMapper.selectMenuList(menu);
    }

    /**
     * 根据mid查询菜单
     * @param parentId
     * @return
     */
    @Override
    public SysMenu selectMenuById(Long parentId) {
        return menuMapper.selectMenuById(parentId);
    }

    /**
     * 检验菜单名字是否存在
     * @param menu
     * @return
     */
    @Override
    public String checkMenuNameUnique(SysMenu menu) {
        SysMenu info = menuMapper.checkMenuNameUnique(menu.getMenuName(), menu.getParentId());
        if (StringUtils.isNotNull(info) )
        {
            return UserConstants.MENU_NAME_NOT_UNIQUE;
        }
        return UserConstants.MENU_NAME_UNIQUE;
    }

    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @Override
    public int insertMenu(SysMenu menu) {

        menu.setCreateBy(ShiroUtils.getLoginName());
        return menuMapper.insertMenu(menu);
    }

    /**
     * 查父菜单的名字
     * @param parentId
     * @return
     */
    @Override
    public String selectParentNameByPid(Long parentId) {
        return menuMapper.selectParentNameByPid(parentId);
    }


    /**
     * 修改菜单
     * @param menu
     * @return
     */
    @Override
    public int updateMenu(SysMenu menu) {
        return menuMapper.updateMenu(menu);
    }


    /**
     * 根据菜单id删除子菜单和自己
     * @param menuId
     * @return
     */
    @Override
    public int removeMenu(Long menuId) {

        int  index=-1;
        try {

            //查询pid
            SysMenu sysMenu = menuMapper.selectMenuById(menuId);

            
              index=  menuMapper.removeMenu(menuId);


            //删除role_menu
               index=menuMapper.deleteRoleMenuByMid(menuId,sysMenu.getParentId() ) ;
        } catch (Exception e) {
            e.printStackTrace();
        }
            return  index;

    }


    /**
     * 查询所有的菜单
     * @return
     */
    private List<SysMenu> selectMenuAll() {
        List<SysMenu>    menuList  = menuMapper.selectMenuAll();
        return     menuList;

    }

    /**
     * 对象转菜单树
     *
     * @param menuList 菜单列表
     * @param roleMenuList 角色已存在菜单列表
     * @param permsFlag 是否需要显示权限标识
     * @return 树结构列表
     */
    public List<Ztree>  initZtree(List<SysMenu> menuList, List<String>  roleMenuList, boolean permsFlag)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();

        for (SysMenu menu : menuList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(menu.getMenuId());
            ztree.setpId(menu.getParentId());
           ztree.setName(transMenuName(menu, permsFlag));
            ztree.setTitle(menu.getMenuName());

            ztrees.add(ztree);
        }
        return  ztrees;
    }



    /**
     * 添加perms
     */
    public String transMenuName(SysMenu menu, boolean  permsFlag)
    {
        StringBuffer sb = new StringBuffer();
        sb.append(menu.getMenuName());
        if (permsFlag)
        {
            sb.append("<font color=\"#888\">&nbsp;&nbsp;&nbsp;" + menu.getPerms() + "</font>");
        }
        return sb.toString();
    }
}
